<%@ page language="java" contentType="text/html; charset=UTF-8" import="org.foreveross.que.infra.base.constant.SysConstants"
	pageEncoding="UTF-8"%>
	<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!--ROWS TEMPLETE  {i} {title} {replyCount} {createTime}  {browseCount} {subjectInfo} {tid}-->
	<div style="display: none" id="rowTemplete">
		<div class="accordion-group">
 			<div class="accordion-heading">
				<a class="accordion-toggle accordion-pic" data-toggle="collapse"
					data-parent="#accordion_{type}" href="#collapse{type}{i}"> 
					<div class="row">
					<div class="col-lg-7 col-sm-6 col-xs-11"><i class="icon-picture" data-toggle-icon="icon-picture icon-picture"></i>{title} {image}</div>
					<div class="col-lg-4 col-sm-5 col-xs-11 text-right" ><div><table align="right"><tr><td align="left" ><font color="blue" size="2px">{lastTime}<br>{createTime}</font></td>
					<td align="right">
					<button title="该话题的回复次数，点击回复该话题" onclick="toChat({tid});" class="btn btn-mini btn-primary"> <i class="icon-edit"></i> {replyCount} </button> 
					<button title="该话题翻阅次数"  class="btn btn-mini btn-primary"> <i class="icon-eye-open"></i>  {browseCount} </button> 
					<c:if test="${vo.authority eq '1' }">
					<button onclick="isStickie({tid},{isStickieType});" class="btn btn-mini btn-primary"><font size="1">{isStickieText}</font></button>
					</c:if>
					</td>
					</tr></table>
					</div>
					</div>
					</div>
				</a>
			</div>
			<div id="collapse{type}{i}" class="accordion-body collapse {in}">
				<div class="accordion-inner">{replyInfo}</div>
			</div>
		</div>
	</div>
	

  <!-- widget stats -->
  <div class="widget border-cyan" id="widget-stats">
    <!-- widget header -->
    <div class="widget-header bg-cyan">
        <!-- widget icon -->
        <div class="widget-icon"><i class="icon-comments-alt"></i></div>
        <!-- widget title -->
        <h4 class="widget-title">热门问题</h4>
        <!-- widget action, you can also use btn, btn-group, nav-tabs or nav-pills (also support dropdown). enjoy! -->
        <div class="widget-action color-cyan">
        <ul class="nav nav-tabs">
            <li class="active"><a data-toggle="tab" title="最近有人回复的话题" id="oneABtn" onclick="findRows('one');" href="#one">最新回复</a></li>
            <li><a data-toggle="tab" id="twoABtn" title="回复最多的话题" onclick="findRows('two');" href="#two">最多回复</a></li>
            <li id='my_menu_li' style="display: ${empty vo.username?'none':''}"><a data-toggle="tab" id="treeABtn" title="只看我提问的话题" onclick="findRows('three');" href="#three">只看我的</a></li>
            <li><a data-toggle="tab" id="fourABtn" title="最新创建的话题" onclick="findRows('four');" href="#four">最新创建</a></li>
            <li> <button data-toggle="collapse" data-collapse="#widget-stats" class="btn btn-up" >
                <i class="icon-minus" data-toggle-icon="icon-minus icon-plus"></i>
            </button></li>
         </ul>
                                                
      </div>
      
        
    </div><!-- /widget header -->
    
    <div class="widget-content">
          <div class="tab-content">
                                                <!-- posts -->
             <div class="tab-pane fade in active" id="one">
       		
           <div class="accordion" id="accordion_one">
           
			</div>
			
           <!--分页 -->
           <ul id="pageIndexs_one" class="pagination pagination-sm pull-right">
                  
          </ul>
            
       </div>
      
        
     <div class="tab-pane fade" id="two">
       <div class="accordion" id="accordion_two">
        </div>  
         <!--分页 -->
           <ul id="pageIndexs_two" class="pagination pagination-sm pull-right">
                  
          </ul>
     </div>
     
      <div class="tab-pane fade" id="three">
       <div class="accordion" id="accordion_three">
        </div>  
         <!--分页 -->
           <ul id="pageIndexs_three" class="pagination pagination-sm pull-right">
                  
          </ul> 
        </div>
        
        <div class="tab-pane fade" id="four">
       <div class="accordion" id="accordion_four">
        </div>  
         <!--分页 -->
           <ul id="pageIndexs_four" class="pagination pagination-sm pull-right">
                  
          </ul> 
        </div>
        <!-- /row-fluid -->
    </div><!-- /widget content -->
    
</div>
  
  <!-- /widget stats -->
                            
</DIV>

<!--/.page-content-->	
	
	
<script>

<c:if test="${vo.authority eq '1' }">
function isStickie(tid,type){
	
	var formData  = $('#search_form').serialize();
	formData += "&vo.topicVo.id=" + tid;
	formData += "&vo.topicVo.isStickie=" + type;
	$.ajax({
		type : 'post',
		cache : false,
		dataType : 'json',
		url : "${pageContext.request.contextPath}/help/topic/isStickie.action",
		data : formData,
		success : function(data) {
			findRows("",1);
			//alert(data);
		},
		error : function(message) {
			alert("操作出错");
		}
	});
}
</c:if>



	var currStart = 1;
	var currPage = 1;
	var showIndexCount = 7;
	var currEnd = showIndexCount;
	

/**
 * 这里使用异步查询模板匹配航记录
 */
	function findRows(type,pageIndex){
		var searchType=$("#searchType").val();
		if($("#chatPageContent").is(":visible")==true){//正在回复
			alert("请先返回吧");
			return ;
		}
		
		
	if(type!=''&&type!=undefined){
		searchType = type;
		$("#searchType").val(searchType);
	}
	var pageI = 1;
	if(pageIndex!=undefined&&pageIndex!=''){
		pageI=pageIndex;
	}

	$("#pageIndex").val(pageI);//设置搜索form域

		var url = "${pageContext.request.contextPath}/help/topic/search.action";
		var formData = $('#search_form').serialize();
	
		$.ajax({
			type : 'post',
			cache : false,
			dataType : 'json',
			url : url,
			loading : '正在查询...',
			data : formData,
			success : function(data) {
				$("#accordion_"+searchType).html("");
				if(data.state==0){
					alert("页面过期");
				}else{
					doCallback(combineTopicHtml,[data,searchType]);
				}
			},
			error : function(message) {
				alert("查询出错");
			}
		});
		
	}
	
	function combineTopicHtml(data,searchType){
		var rowHtml = "--";
		var templete = $("#rowTemplete").html();
		for(var i=0;i<data.result.length;i++){
			var row = data.result[i];
			rowHtml = templete.replaceAll("{i}",i).replaceAll("{title}",row.title).replaceAll("{replyCount}",row.replyCount);
			rowHtml = rowHtml.replaceAll("{createTime}",row.createTime.replace("T"," "));
			rowHtml = rowHtml.replaceAll("{browseCount}",row.browseCount);
			
			if(row.lastTime!=undefined&&row.lastTime!=""){
				rowHtml = rowHtml.replaceAll("{lastTime}",row.lastTime.replace("T"," "));
			}else{
				rowHtml = rowHtml.replaceAll("{lastTime}","");
			}
			var replyInfo = $.trim(row.replyInfo);
			if(replyInfo!='null'&&replyInfo!=''){
				rowHtml = rowHtml.replaceAll("{replyInfo}",replyInfo);
			}else{
				rowHtml = rowHtml.replaceAll("{replyInfo}",'还没有最优回复，快去抢一个吧。');
			}
			rowHtml = rowHtml.replaceAll("{type}",searchType);
			rowHtml = rowHtml.replaceAll("{tid}",row.id);
			<c:if test="${vo.authority eq '1' }">
			
			if(row.isStickie==1){
				rowHtml = rowHtml.replaceAll("{isStickieType}",0);
				rowHtml = rowHtml.replaceAll("{isStickieText}","取消置顶");
			}else{
				rowHtml = rowHtml.replaceAll("{isStickieType}",1);
				rowHtml = rowHtml.replaceAll("{isStickieText}","置顶");
			}
			</c:if>
			if(row.isStickie==1){
				rowHtml = rowHtml.replaceAll('{image}','<img src="${pageContext.request.contextPath}/images/fire.png">');
			}else{
				rowHtml = rowHtml.replaceAll('{image}','');
			}
			if(i==0){
				rowHtml = rowHtml.replaceAll("{in}","in");
			}else{
				rowHtml = rowHtml.replaceAll("{in}","");
			}
			$("#accordion_"+searchType).append(rowHtml);
		}
		if(data.result.length==0){
			$("#accordion_"+searchType).html("没有相关问题呀，提个问题吧！");
			$("#pageIndexs_"+searchType).html("");
		}else{
			var showIndexs = getShowPageIndex(data.pageIndex,data.pageCount);
			$("#pageIndexs_"+searchType).html(showIndexs);
		}
	}


	function getShowPageIndex(changePage,pageCount,fnName){
		var param1 = "";
		if(fnName==undefined){
			fnName="findRows";
			param1 = "\'\',";
		}
		var html = "";
		var start = 1;
		var end = 1;
		if(changePage==1){
			html += '<li class="disabled"><a href="javascript:void();">&laquo;</a></li>';
		}else{
			html += '<li><a href="javascript:'+fnName+'('+param1+(changePage-1)+');">&laquo;</a></li>';
		}
		if(pageCount<showIndexCount){
			end = pageCount;
		}else{
			var currMid = currStart+Math.floor(showIndexCount/2);
			if((changePage-currMid)>(showIndexCount/4)){
				end = changePage+Math.floor(showIndexCount/2);
			}else if((currMid-changePage)>(showIndexCount/4)){
				end = changePage+Math.floor(showIndexCount/1.5);
			}else{
				//页数标号不变
				end = currEnd;
			}
			end = end>pageCount?pageCount:end;
			end = end<showIndexCount?showIndexCount:end;
			
			start = end - showIndexCount+1;
		}
		
		for(var i=start;i<=end;i++){
			if(changePage==i){
				html += '<li class="active"><a href="javascript:void();">'+i+'</a></li>';
			}else{
				html += '<li><a href="javascript:'+fnName+'('+param1+i+')">'+i+'</a></li>';;
			}
		}
		if(changePage==pageCount){
			html += '<li class="disabled"><a href="javascript:void();">&raquo;</a></li>';	
		}else{
			html += '<li><a onclick="" href="javascript:'+fnName+'('+param1+(changePage+1)+');">&raquo;</a></li>';
		}
		currPage = changePage;
		currEnd = end;
		currStart = start;
		return html;
	}
	
	function toChat(tid){
		$("#topicPageContent").hide();
		$("#chatPageContent").show();
		withOutTopic = false;
		findChatRows(1,tid);//help_chat
	}
</script>		 
